<?php

namespace App\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PackageContentModel extends Model
{
    use HasFactory;

    protected $table = 'package_content';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    static public function getPackageContentInfo($keyword, $data)
    {
        return PackageContentModel::select(
            'a.id',
            'package_id'
        )
            ->from('package_content as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'c.id', '=', 'b.parent_id')
            ->leftJoin('package as d', 'd.id', '=', 'a.package_id')
            ->where('a.main_id', $data['main_id'])
            ->where(function($query) use ($keyword) {
                $query->orWhere('materiel_class_name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('materiel_first_class_name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('materiel_first_class_no', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('materiel_class_no', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('c.class_name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('c.class_no', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('materiel_name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('type_name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('materiel_no', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('d.package_name', 'LIKE', '%'.$keyword.'%');
            })
            ->get()->toArray();
    }

    static public function checkPackageContent($packageId, $storageManageInfo)
    {
        return SELF::where([
            'package_id' => $packageId,
            'storage_manage_id' => $storageManageInfo['id'],
            'type_id' => $storageManageInfo['type_id'],
            'storage_id' => $storageManageInfo['storage_id'],
            'delete_flag' => 0
        ])->first();
    }

    static public function getSingleContentInfo($id)
    {
        return SELF::select(
            'id',
            'materiel_class_id',
            'type_id',
            'storage_id',
            'storage_manage_id',
            'num'
        )
            ->where([
                'package_id' => $id,
                'delete_flag' => 0
            ])
            ->get()->toArray();
    }

    static public function getContentInfo($data)
    {
        return SELF::select(
            'a.id',
            'b.class_name as materiel_first_class_name',
            'b.class_no as materiel_first_class_no',
            'c.class_name as materiel_class_name',
            'c.class_no as materiel_class_no',
            'd.materiel_name',
            'd.type_name',
            'd.materiel_no',
            'a.storage_id',
            'e.storage_name',
            'e.storage_address',
            'a.storage_manage_id',
            'a.package_id',
            'a.num',
            'f.in_storage_num',
            'f.delete_flag'
        )
            ->from('package_content as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_first_class_id')
            ->leftJoin('materiel_class as c', 'c.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_type as d', 'd.id', '=', 'a.type_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage_manage as f', 'f.id', '=', 'a.storage_manage_id')
            ->where('package_id', $data['id'])
            ->where('a.delete_flag', 0)
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }
}
